//
//  WXLifeViewController.m
//  NewGuiYang
//
//  Created by 罗江 on 2018/5/25.
//  Copyright © 2018年 luojiang. All rights reserved.
//

#import "WXLifeViewController.h"

#import <WXXHNInternetSupervise/WXInternetSuperviseViewConfigManager.h>
#import <WXXHNInternetSupervise/WXGovermentHomeView.h>
#import <WXXHNInternetSupervise/WXLivelihoodHomeView.h>
#import <WXXHNInternetSupervise/WXLivelihoodViewController.h>
#import <WXXHNInternetSupervise/WXGovermentPublicViewHomeController.h>
#import <WXXHNInternetSupervise/WXGovermentBusinessViewModel.h>
#import <WXXHNInternetSupervise/WXLivelihoodListViewModel.h>
#import <WXXHNInternetSupervise/WXGovermentNewsListViewModel.h>
#import <WXXHNInternetSupervise/WXWebServiceCollectionView.h>

#import <WXCloudModuleBaseConfig/WXModuleDataConfigManager.h>
#import <WXCloudModuleBaseConfig/WXSystemBaseDefine.h>

#import <WXCloudModuleCategories/UIView+LayoutMethods.h>
#import <WXCloudModuleCategories/UIApplication+ActivityViewController.h>
#import <WXCloudModuleCategories/UIView+UIStyle.h>

#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleSearchActions.h>
#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleNewsDetailActions.h>
#import <WXCloudModuleMediator/WXMediator+WXMediatorModulePoliticsDetailActions.h>
#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleNoNetWorkActions.h>

#import <WXCloudModuleBaseView/WXImageButton.h>
#import <WXCloudModuleBaseView/WXNavigationBaseView.h>

#import <WXCloudModuleNetworking/WXNoNetWorkView.h>
#import <WXCloudModuleNetworking/WXAppContext.h>
#import <WXCloudModuleNetworking/UIView+NoNetWorkView.h>

#import <WXCloudModuleCacheManager/WXBaseInteractiveModel.h>

#import <WXCloudModuleCustomView/WXNewsBaseTableView.h>

#import <WXCloudModuleCommonNewList/WXCommonNewsViewModel.h>

#import <WXCloudModuleTableViewCell/WXCommonNewsTableViewModel.h>

@interface WXLifeViewController ()<WXNoNetWorkViewDelegate,WXNewsBaseTableViewDelegate>

@property (nonatomic, strong) WXNavigationBaseView *titleView;

@property (nonatomic, strong) WXNewsBaseTableView *tableView;

@property (nonatomic, strong) WXWebServiceCollectionView *webServiceHomeView;

@property (nonatomic, strong) WXNoNetWorkView *noNetWorkingView;
/** 生活服务ViewModel*/
@property (nonatomic, strong) WXCommonNewsViewModel *webServiceViewModel;
/** 信息查询ViewModel*/
@property (nonatomic, strong) WXCommonNewsViewModel *infoSearchViewModel;

@property (nonatomic, strong) RACSubject *modelSelectSubject;
@end

@implementation WXLifeViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialize];
    [self addReactiveObserve];
}
- (void)addReactiveObserve
{
    @weakify(self)
    [[[RACSignal combineLatest:@[RACObserve(_webServiceViewModel, homeNewsModelList), RACObserve(_infoSearchViewModel, homeNewsModelList)] reduce:^id(NSArray *webServiceList, NSArray *infoSearchList){
        return @(webServiceList.count>0 && infoSearchList.count>0);
    }] filter:^BOOL(NSNumber *value) {
        return value.boolValue == YES;
    }] subscribeNext:^(id x) {
        @strongify(self)
        self.webServiceHomeView.webServiceList = self.webServiceViewModel.homeNewsModelList;
        self.webServiceHomeView.infoSearchList = self.infoSearchViewModel.homeNewsModelList;
        [self.webServiceHomeView reloadData];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.webServiceHomeView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width,self.webServiceHomeView.collectionViewLayout.collectionViewContentSize.height);
             self.tableView.tableFooterView = self.webServiceHomeView;
        });
    }];
    
    [_modelSelectSubject subscribeNext:^(WXCommonNewsTableViewModel *model) {
        @strongify(self);
        if (model!=nil) {
            UIViewController *targerVC = [[WXMediator sharedInstance] WXMediator_showNewsWithModelAction:model];
            [self.navigationController pushViewController:targerVC animated:YES];
        }
    }];
}

- (void)initialize
{
    _modelSelectSubject = [RACSubject subject];
    _webServiceViewModel = [WXCommonNewsViewModel new];
    _infoSearchViewModel = [WXCommonNewsViewModel new];
    [_webServiceViewModel requestHomeNewsWithClassID:@"3614" requestType:WXNewsRequestTypeFirst];
    [_infoSearchViewModel requestHomeNewsWithClassID:@"3615" requestType:WXNewsRequestTypeFirst];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = self.webServiceHomeView;
    [self.view addSubview:self.titleView];
    if ([WXAppContext sharedInstance].isReachable) {
        if (self.noNetWorkingView !=nil) {
            [self.noNetWorkingView removeFromSuperview];
        }
    } else {
        self.noNetWorkingView = [self.tableView addNoNetWorkingView];
        self.noNetWorkingView.delegate=self;
    }
}

#pragma mark WXNoNetWorkViewDelegate代理方法
/** 顶部按钮代理 */
- (void)noNetWorkViewdidClickNoNetWorkTopButton
{
    UIViewController *targetVC= [[WXMediator sharedInstance] WXMediator_getNoNetWorkAction];
    [self.navigationController pushViewController:targetVC animated:YES];
}
- (void)noNetWorkViewdidClickNoNetWorkFullScreenButton{
    if ([WXAppContext sharedInstance].isReachable) {
        if (self.noNetWorkingView !=nil) {
            [self.noNetWorkingView removeFromSuperview];
        }
        [_webServiceViewModel requestHomeNewsWithClassID:@"3614" requestType:WXNewsRequestTypeFirst];
        [_infoSearchViewModel requestHomeNewsWithClassID:@"3615" requestType:WXNewsRequestTypeFirst];
         [self.webServiceHomeView reloadData];
    }else{
        
        
    }
    
}


- (WXNavigationBaseView *)titleView
{
    if (_titleView == nil) {
        _titleView = [[WXNavigationBaseView alloc]initWithTitle:@"生活"];
        _titleView.titleLabel.textColor = [WXInternetSuperviseViewConfigManager sharedInstance].TitleLabelColor;
        _titleView.backgroundColor = [WXInternetSuperviseViewConfigManager sharedInstance].TitleBackgroundColor;
        /** 右上角搜索按钮 */
        //        _titleView.rightBtn=self.rightButton;
        [_titleView addNormalShadow];
        
    }
    return _titleView;
}

-(WXWebServiceCollectionView *)webServiceHomeView{
    if (_webServiceHomeView==nil) {
        _webServiceHomeView = [[WXWebServiceCollectionView alloc]init];
        _webServiceHomeView.modelSelectSubject = _modelSelectSubject;
    }
    return _webServiceHomeView;
}
-(UITableView *)tableView{
    if (_tableView==nil) {
        _tableView = [[WXNewsBaseTableView alloc]initWithFrame:CGRectMake(0, 64, WXScreenWidth, WXScreenHeight-64-50)];
        _tableView.customDelegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end
